package com.sunelec.service.Imp;

import com.sunelec.mapper.RelationshipMapper;
import com.sunelec.mapper.TwoLevelUserMapper;
import com.sunelec.mapper.UsersMapper;
import com.sunelec.pojo.Inverter;
import com.sunelec.pojo.Relationship;
import com.sunelec.pojo.SunElecResult;
import com.sunelec.pojo.Users;
import com.sunelec.service.TwoLevelUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 吴遵明
 * @Description: 二级用户注册Service,和关联三级用户
 * @Date: Create in 2018/05/07
 * @Modified By:
 */
@Service
public class TwoLevelUserServiceImpl implements TwoLevelUserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private TwoLevelUserMapper twoLevelUserMapper;

    @Autowired
    private RelationshipMapper relationshipMapper;

    /**
     * 二级用户注册
     * @param users
     * @return
     */
    @Override
    public SunElecResult createUser(Users users) {
        usersMapper.insert(users);
        return SunElecResult.ok();
    }

    /**
     * 根据权限查询所有用户
     * @param type
     * @return
     */
    @Override
    public List<Users> selectUsers(int type) {
        return twoLevelUserMapper.selectUsers(type);
    }

    /**
     * 根据id更新user
     * @param user
     * @return
     */
    @Override
    public int updateUserById(Users user) {
        return usersMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 查询2级用户，未被关联的3级用户(用户有多个电站是不会重复的，这里只显示用户)
     * @param type
     * @return
     */
    @Override
    public List<Users> selectAssociateUser(int type) {
        return twoLevelUserMapper.selectAssociateUser(type);
    }

    //根据所有3级leveluser获得relationship<list>
    public List<Relationship> selectRelationshipByLeveluser(List<String> leveluser) {
        return twoLevelUserMapper.selectRelationshipByLeveluser(leveluser);
    }

    //2级用户关联3级用户
    public void updateRelationship(Relationship relationship) {
        relationshipMapper.updateByPrimaryKey(relationship);
    }
}
